import axios from 'axios'
// 获取token
function getToken(){
    return sessionStorage.getItem('token')
}
// 创建axios示例
let service = axios.create({
    baseURL:'地址',
    timeout:6000
})
// 请求拦截
service.interceptors.request.use(
    // 成功
    config =>{
        if(getToken()){
            config.headers['token'] = getToken()
        }
        return config
    },
    // 失败
    err =>{
        return Promise.reject(err)
    }
)
// 响应拦截
service.interceptors.response.use(
    reponse =>{
        let res = reponse.data
        if(res.code == '401'){
            location.href = '/login'
        }
        return Promise.resolve(res)
    },
    err =>{
        return Promise.reject(res)
    }
)
export default service